package render {
	import flare.util.Property;
	import flare.util.Shapes;
	import flare.vis.data.DataSprite;
	import flare.vis.data.render.ShapeRenderer;
    import flare.util.IEvaluable;

	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;


	public class IconRenderer extends ShapeRenderer {
		public static const SMALL:int = 4;
		public static const MEDIUM:int = 6;
		public static const LARGE:int = 8;
		public static const DEFAULT_SIZE:int = 6;
		protected var _source:Property;
		public function get source():String { return _source.name; }
		public function set source(s:String):void { _source = Property.$(s); }
		public var iconFunction:Function = null;
		/**
		 * Creates a new ShapeRenderer
		 * @param defaultSize the default size (radius) for shapes
		 */
		public function IconRenderer(source:*=null, defaultSize:Number=6) {
			super(defaultSize);
			// this.defaultSize = defaultSize;
			if (source is String) {
				_source = Property.$(source);
			} else if (source is Property) {
				_source = Property(source);
			} else if (source is IEvaluable) {
				iconFunction = IEvaluable(source).eval;
			} else if (source is Function) {
				iconFunction = source;
			}
		}
		private function renderHelper(d:DataSprite, bitmapData:BitmapData):void {
			// super.render(d);
			var lineAlpha:Number = d.lineAlpha;
			var fillAlpha:Number = d.fillAlpha;
			var size:Number = d.size * defaultSize;
			
			var g:Graphics = d.graphics;
			g.clear();
			// trace('> ' + bitmapData.height + ':' + bitmapData.width);
			// trace('> ' + defaultSize);
			// trace(' >' + size);
			g.beginFill(0xFFFFFF);
			// var roundSize:Number = size + 2;
			g.drawRect(-size, -size, 2*size, 2*size);


			// if (fillAlpha > 0) g.beginFill(d.fillColor, fillAlpha);
			var matrix:Matrix = new Matrix();
			var sx:Number = Number(2*size) / (bitmapData.width);
			var sy:Number = Number(2*size) / (bitmapData.height);
			matrix.scale(sx, sy);
			matrix.translate(-size, -size);
			if (bitmapData) g.beginBitmapFill(bitmapData, matrix, false);
			if (lineAlpha > 0) g.lineStyle(d.lineWidth, d.lineColor, lineAlpha);
		
			Shapes.getShape(d.shape)(g, size);
/*	
			switch (d.shape) {
				case null:
					break;
				case Shapes.BLOCK:
					g.drawRect(d.u-d.x, d.v-d.y, d.w, d.h);
					break;
				case Shapes.POLYGON:
					if (d.points!=null)
						Shapes.drawPolygon(g, d.points);
					break;
				case Shapes.POLYBLOB:
					if (d.points!=null)
						Shapes.drawCardinal(g, d.points,
							d.points.length/2, 0.15, true);
					break;
				case Shapes.VERTICAL_BAR:
					g.drawRect(-size/2, -d.h, size, d.h);
					break;
				case Shapes.HORIZONTAL_BAR:
					g.drawRect(-d.w, -size/2, d.w, size);
					break;
				case Shapes.WEDGE:
					Shapes.drawWedge(g, d.origin.x-d.x, d.origin.y-d.y,
						d.h, d.v, d.u, d.u+d.w);
					break;
				default:
			}
*/
			
			// if (fillAlpha > 0) g.endFill();
			if (bitmapData) g.endFill();
			
			var filter1:DropShadowFilter = new DropShadowFilter();
			filter1.angle = 60;
			filter1.blurX = 4;
			filter1.blurY = 4;
			filter1.distance = 5;
			filter1.strength = 0.35;
			filter1.quality = BitmapFilterQuality.MEDIUM;
			trace('> ' + d.data.id + ' ' + d.filters + ' ' + d.filters.length);
			if (d.filters.length > 0) d.filters.push(filter1);
			else d.filters = [filter1];
		}
		
		public override function render(d:DataSprite):void {
			var ldr:Loader = new Loader();
			var url:String = getIconLocation(d);
			var request:URLRequest = new URLRequest(url);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void {
				var loader:Loader = event.target.loader as Loader; // new Loader(event.target.loader);
				trace('> ' + loader);
				var image:Bitmap = loader.content as Bitmap;
				var bitmapData:BitmapData = image.bitmapData;
				// var tmp:BitmapData = new BitmapData(this.defaultSize, this.defaultSize);
				// tmp.draw(bitmapData);
				// bitmapData.width = this.defaultSize;
				// bitmapData.height = this.defaultSize;
				renderHelper(d, bitmapData);
				// renderHelper(d, tmp);
			});
			ldr.load(request);
		}
		public function getIconLocation(d:DataSprite):String {
			if (iconFunction != null) {
				return iconFunction(d);
			} else {
				return _source.getValue(d);
			}
		}
	}
}
